package com.infoDiffusion.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.infoDiffusion.bean.EarthquakeBean;
import com.infoDiffusion.bean.EarthquakeBean;
import com.infoDiffusion.bean.UserBean;
import com.infoDiffusion.service.EarthquakeStatService;
import com.infoDiffusion.service.ReportersService;
import com.infoDiffusion.util.Constants;
import com.infoDiffusion.util.StringUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class EarthquakeStatAction extends ActionSupport {
	@Autowired
	EarthquakeStatService estatService;
	EarthquakeBean estatBean;
	UserBean bean;
	public HttpServletResponse response = ServletActionContext.getResponse();
	public HttpServletRequest request = ServletActionContext.getRequest();
	public HttpSession session = request.getSession();

	// 添加地震知识
	public String add() {
		bean = (UserBean) request.getSession().getAttribute(Constants.SESSION_ADMIN);
		estatBean.setUserBean(bean);
		System.out.println(estatBean);
		boolean flag = estatService.add(estatBean);
		if (flag)
			return "addsucc";
		else
			return "addfals";
	}

	// 地震信息列表
	public String list() {
		bean = (UserBean) request.getSession().getAttribute(Constants.SESSION_ADMIN);
		ActionContext.getContext().put("bean", bean);
		List<EarthquakeBean> list = estatService.list(estatBean);
		ActionContext.getContext().put("list", list);
		return "list";
	}

	// 获取ID及bean数据
	public String toUpdate() {
		int id = StringUtil.toInt(request.getParameter("id"));
		if (id > 0) {
			EarthquakeBean bean = estatService.getById(id);
			ActionContext.getContext().put("bean", bean);
		}
		return "toUpdate";

	}

	// 更新地震知识
	public String update() {
		boolean flag = estatService.update(estatBean);
		if (flag)
			return "updateok";
		else
			return "updatefalse";
	}

	// 删除地震知识
	public String delete() {
		int id = StringUtil.toInt(request.getParameter("id"));
		if (id > 0) {
			EarthquakeBean bean = estatService.getById(id);
			boolean flag = estatService.delete(bean);
			if (flag)
				return "delete";
		}
		return "flase";
	}

	public EarthquakeBean getEstatBean() {
		return estatBean;
	}

	public void setEstatBean(EarthquakeBean estatBean) {
		this.estatBean = estatBean;
	}

	public UserBean getBean() {
		return bean;
	}

	public void setBean(UserBean bean) {
		this.bean = bean;
	}

}